﻿package com.wandian.util.scrollBar
{
	import com.wandian.util.CLZButton;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

    public class ScrollBarMc extends Sprite
    {
        private var bottom:SimpleButton;
        private var up:SimpleButton;
        private var down:SimpleButton;
        private var slideBtn:MovieClip;
        private var sign:MovieClip;
        private var slide:Sprite;
        private var h:Number;
        private var upperLimit:Number;
        private var lowerLimit:Number;
        private var _value:int;
        private var direction:Number;
        private var target:DisplayObject;
        private var type:String;
        private var _rect:Rectangle;
        private var stretch:Boolean;
        private var maskSprite:Sprite;
        public static const EVENT_SLIPPAGE:String = "slippage";
        public static const TYPE_LEVEL:String = "type_level";
        public static const TYPE_VERTICAL:String = "type_vertical";

        public function ScrollBarMc(param1:Rectangle, _target:DisplayObject, arrButton:Array, _type:String = "type_vertical", sx:Number = 0, sy:Number = 0, boolStretch:Boolean = true)
        {
            this.slide = new Sprite();
            this.maskSprite = new Sprite();
            this.target = _target;
            this.type = _type;
            this.x = sx;
            this.y = sy;
            this.stretch = boolStretch;
            this.maskSprite.mouseChildren = false;
            this.maskSprite.mouseEnabled = false;
            _target.parent.addChild(this.maskSprite);
            _target.parent.addChild(this);
            _target.mask = this.maskSprite;
            this.bottom = arrButton[0] as SimpleButton;
            this.up = arrButton[1] as SimpleButton;
            this.down = arrButton[2] as SimpleButton;
            this.slideBtn = arrButton[3] as MovieClip;
            CLZButton.setButton(this.slideBtn);
            this.slideBtn.mouseChildren = false;
            this.sign = arrButton[4] as MovieClip;
            this.sign.buttonMode = true;
            this.slideBtn.scale9Grid = new Rectangle(this.slideBtn.width / 3, this.slideBtn.height / 3, this.slideBtn.width / 3, this.slideBtn.height / 3);
            this.rect = param1;
            this.addChild(this.bottom);
            this.addChild(this.up);
            this.addChild(this.down);
            this.addChild(this.slide);
            this.slide.addChild(this.slideBtn);
            this.slide.addChild(this.sign);
            this.refresh();
            if (stage)
            {
                this.init();
            }
            else
            {
                this.addEventListener(Event.ADDED_TO_STAGE, this.init);
            }
            return;
        }

        private function init(event:Event = null) : void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, this.init);
            this.up.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDwon_up);
            this.down.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDwon_dwon);
            this.slide.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownSlide);
            this.addEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
            this.target.addEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
            return;
        }

        private function onEnterFrame(event:Event) : void
        {
            this.slide.y = this.slide.y + this.direction * 3;
            if (this.slide.y < this.upperLimit)
            {
                this.slide.y = this.upperLimit;
            }
            if (this.slide.y > this.lowerLimit + this.upperLimit)
            {
                this.slide.y = this.lowerLimit + this.upperLimit;
            }
            this.move();
            return;
        }

        private function onMouseDwon_up(event:MouseEvent) : void
        {
            this.direction = -1;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpStage);
            this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            return;
        }

        private function onMouseDwon_dwon(event:MouseEvent) : void
        {
            this.direction = 1;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpStage);
            this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            return;
        }

        private function onMouseDownSlide(event:MouseEvent) : void
        {
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpStage);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoves);
            this.slide.startDrag(false, new Rectangle(0, this.upperLimit, 0, this.lowerLimit));
            return;
        }

        private function onMouseUpStage(event:MouseEvent) : void
        {
            var event:* = event;
            try
            {
                this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
                stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpStage);
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoves);
            }
            catch (e:Error)
            {
            }
            this.slide.stopDrag();
            return;
        }

        private function onMouseMoves(event:MouseEvent) : void
        {
            this.move();
            event.updateAfterEvent();
            return;
        }

        private function move() : void
        {
            this._value = Math.round((this.slide.y - this.upperLimit) / this.lowerLimit * 100);
            if (this.type == TYPE_LEVEL)
            {
                if (this.target.width > this.rect.width)
                {
                    this.target.x = this.rect.x - (this.target.width - this.rect.width) * (this._value / 100);
                }
                else
                {
                    this.target.x = this.rect.x;
                }
            }
            else if (this.type == TYPE_VERTICAL)
            {
                if (this.target.height > this.rect.height)
                {
                    this.target.y = this.rect.y - (this.target.height - this.rect.height) * (this._value / 100);
                }
                else
                {
                    this.target.y = this.rect.y;
                }
            }
            this.dispatchEvent(new Event(EVENT_SLIPPAGE));
            return;
        }

        public function get value() : int
        {
            return this._value;
        }

        public function set value(param1:int) : void
        {
            if (param1 < 0)
            {
                param1 = 0;
            }
            if (param1 > 100)
            {
                param1 = 100;
            }
            this.slide.y = this.lowerLimit * (param1 / 100) + this.upperLimit;
            this.move();
            return;
        }

        private function onMouseWheel(event:MouseEvent) : void
        {
            this.value = this.value - event.delta * 3;
            return;
        }

        public function get rect() : Rectangle
        {
            return this._rect;
        }

        public function set rect(param1:Rectangle) : void
        {
            this._rect = param1;
            this.maskSprite.graphics.clear();
            this.maskSprite.graphics.beginFill(16711680, 0);
            this.maskSprite.graphics.drawRect(this._rect.x, this._rect.y, this._rect.width, this._rect.height);
            this.target.x = param1.x;
            this.target.y = param1.y;
            if (this.type == TYPE_LEVEL)
            {
                this.rotation = -90;
                this.h = param1.width;
            }
            else if (this.type == TYPE_VERTICAL)
            {
                this.rotation = 0;
                this.h = param1.height;
            }
            else
            {
                throw new Error("滚动条类型错误");
            }
            this.bottom.y = this.up.height;
            this.bottom.height = this.h - (this.up.height + this.down.height);
            this.slide.y = this.up.height;
            this.down.y = this.up.height + this.bottom.height;
            this.down.y = this.up.height + this.bottom.height;
            this.refresh();
            return;
        }

        public function refresh() : void
        {
            if (this.type == TYPE_LEVEL)
            {
                if (this.stretch)
                {
                    this.slideBtn.height = (this.h - this.up.height - this.down.height) / (this.target.width / this.h);
                    this.sign.y = (this.slideBtn.height - this.sign.height) * 0.5;
                    this.sign.x = (this.slideBtn.width - this.sign.width) * 0.5;
                    if (this.slideBtn.height <= this.sign.height)
                    {
                        this.sign.visible = false;
                    }
                    else
                    {
                        this.sign.visible = true;
                    }
                }
                var _loc_1:* = this.target.width > this.rect.width;
                this.slide.visible = this.target.width > this.rect.width;
                if (_loc_1)
                {
                    this.value = 100 * ((this.rect.x - this.target.x) / (this.target.width - this.rect.width));
                }
                else
                {
                    this.value = 0;
                }
            }
            else if (this.type == TYPE_VERTICAL)
            {
                if (this.stretch)
                {
                    this.slideBtn.height = (this.h - this.up.height - this.down.height) / (this.target.height / this.h);
                    this.sign.y = (this.slideBtn.height - this.sign.height) * 0.5;
                    this.sign.x = (this.slideBtn.width - this.sign.width) * 0.5;
                    if (this.slideBtn.height <= this.sign.height)
                    {
                        this.sign.visible = false;
                    }
                    else
                    {
                        this.sign.visible = true;
                    }
                }
                var _loc_1:* = this.target.height > this.rect.height;
                this.slide.visible = this.target.height > this.rect.height;
                if (_loc_1)
                {
                    this.value = 100 * ((this.rect.y - this.target.y) / (this.target.height - this.rect.height));
                }
                else
                {
                    this.value = 0;
                }
            }
            this.lowerLimit = this.bottom.height - this.slide.height;
            this.upperLimit = this.up.height;
            this.onEnterFrame(null);
            return;
        }

        public function disuse() : void
        {
            this.slide.stopDrag();
            this.up.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDwon_up);
            this.down.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDwon_dwon);
            this.slide.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownSlide);
            this.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
            this.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            if (this.target != null)
            {
                this.target.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseWheel);
            }
            if (stage != null)
            {
                stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpStage);
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoves);
            }
            return;
        }

    }
}
